package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.take;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.NetWorkUtils;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalTakeIndexBinding;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalTakeIndexPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalTakeIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.door.put.DoorPutScanActivity;
import cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.HandInputActivity;

public class InHospitalTakeIndexActivity extends BaseActivity<ActivityInHospitalTakeIndexBinding, InHospitalTakeIndexViewModel, InHospitalTakeIndexPresenter> implements InHospitalTakeIndexPresenter.OnReceivePendingNum {
    //PDA扫码广播
    private ScanReceiverHospitalTake mScanReceiver;
    private IntentFilter intentFilter;

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);
        binding.hospitalIndexTitle.titleTextTv.setText("扫码取药");
        binding.hospitalIndexTitle.titleBackTv.setText("");
        binding.hospitalIndexTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.hospitalIndexTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //显示操作人姓名
        if(null != PreferenceManager.getInstance().getLUserName() || "".equals(PreferenceManager.getInstance().getLUserName())){
            binding.tvPharmacyCname.setText("工号: "+ PreferenceManager.getInstance().getLUserNum());
        }
        binding.hospitalIndexTitle.titleMenuConfirm.setVisibility(View.VISIBLE);
        binding.hospitalIndexTitle.titleMenuConfirm.setText("手动输入");
        binding.hospitalIndexTitle.titleMenuConfirm.setTextColor(getResources().getColor(R.color.white));
        binding.hospitalIndexTitle.titleMenuConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(InHospitalTakeIndexActivity.this, HandInputActivity.class);
                startActivityForResult(intent,Contants.REQUEST_CODE);
            }
        });
//        binding.imgvHospitalIndexSaoma.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                presenter.loadData("1833399");
//            }
//        });
        mScanReceiver = new ScanReceiverHospitalTake();
        intentFilter = new IntentFilter();
        intentFilter.addAction(Contants.WOW_NEW_ADD);

        presenter.registerOnReceivePendingNum(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                switch (resultCode){
                    case Contants.RESULT_HAND_SUCCESS_CODE:
                        //TODO:用户未操作完全的药单 按BACK键返回  刷新界面数据     暂时不考虑
                        String  handInput = data.getExtras().getString("handInput");
                        if(null != handInput && !"".equals(handInput.trim()) ){
                            presenter.quey("8",handInput);
                        }
                        break;
                    case Contants.RESULT_HAND_FAIL_CODE:

                        break;
                }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_take_index;
    }

    @Override
    protected Class<InHospitalTakeIndexPresenter> getPresenterClass() {
        return InHospitalTakeIndexPresenter.class;
    }

    @Override
    protected Class<InHospitalTakeIndexViewModel> getViewModelClass() {
        return InHospitalTakeIndexViewModel.class;
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mScanReceiver, intentFilter);
        presenter.setPendingNum("2");
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mScanReceiver);
    }

    @Override
    public void onPending(String onPendingNum) {
        binding.tvHospitalIndexPendingNumber.setText(onPendingNum);
    }

    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiverHospitalTake extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case Contants.WOW_NEW_ADD:
                    String stringExtra = intent.getStringExtra(Contants.WOW_NEW_KEY);
                    //判断口服还是针剂
//                    if(null != stringExtra && stringExtra.length()>1){
//                        if(stringExtra.substring(0,1).equalsIgnoreCase("z")){
//                            String substring = stringExtra.substring(1, stringExtra.length());
//                            presenter.quey("8",substring);
//                        }else{
//                            presenter.quey("8",stringExtra);
//                        }
//                    }
                    if(NetWorkUtils.isWifi(InHospitalTakeIndexActivity.this)){
                        presenter.quey("8",stringExtra);
                    }else{
                        CreateDialog.failDialog3(InHospitalTakeIndexActivity.this,"错误","当前网络不可用，请检查你的网络设置");
                    }
                    break;
            }
        }
    }
}